/*
Copyright [2023] [fuwen_soft]
*/
package vip.xiaonuo.sys.modular.sysappinfoevaluation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import vip.xiaonuo.core.consts.CommonConstant;
import vip.xiaonuo.core.enums.CommonStatusEnum;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.factory.PageFactory;
import vip.xiaonuo.core.pojo.page.PageResult;
import vip.xiaonuo.core.util.PoiUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.sys.modular.sysappinfoevaluation.entity.SysAppInfoEvaluation;
import vip.xiaonuo.sys.modular.sysappinfoevaluation.enums.SysAppInfoEvaluationExceptionEnum;
import vip.xiaonuo.sys.modular.sysappinfoevaluation.mapper.SysAppInfoEvaluationMapper;
import vip.xiaonuo.sys.modular.sysappinfoevaluation.param.SysAppInfoEvaluationParam;
import vip.xiaonuo.sys.modular.sysappinfoevaluation.service.SysAppInfoEvaluationService;

import javax.annotation.Resource;
import java.util.List;

/**
 * app评价service接口实现类
 *
 * @author yanwei
 * @date 2023-03-21 12:07:25
 */
@Service
public class SysAppInfoEvaluationServiceImpl extends ServiceImpl<SysAppInfoEvaluationMapper, SysAppInfoEvaluation> implements SysAppInfoEvaluationService {

    @Override
    public PageResult<SysAppInfoEvaluation> page(SysAppInfoEvaluationParam sysAppInfoEvaluationParam) {
        QueryWrapper<SysAppInfoEvaluation> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(sysAppInfoEvaluationParam)) {

            // 根据评论作者名称 查询
            if (ObjectUtil.isNotEmpty(sysAppInfoEvaluationParam.getAuthor())) {
                queryWrapper.lambda().eq(SysAppInfoEvaluation::getAuthor, sysAppInfoEvaluationParam.getAuthor());
            }
            // 根据评论作者内容 查询
            if (ObjectUtil.isNotEmpty(sysAppInfoEvaluationParam.getAuthorQuote())) {
                queryWrapper.lambda().eq(SysAppInfoEvaluation::getAuthorQuote, sysAppInfoEvaluationParam.getAuthorQuote());
            }
        }
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<SysAppInfoEvaluation> list(SysAppInfoEvaluationParam sysAppInfoEvaluationParam) {
        return this.list();
    }

    @Override
    public void add(SysAppInfoEvaluationParam sysAppInfoEvaluationParam) {
        SysAppInfoEvaluation sysAppInfoEvaluation = new SysAppInfoEvaluation();
        BeanUtil.copyProperties(sysAppInfoEvaluationParam, sysAppInfoEvaluation);
        this.save(sysAppInfoEvaluation);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<SysAppInfoEvaluationParam> sysAppInfoEvaluationParamList) {
        sysAppInfoEvaluationParamList.forEach(sysAppInfoEvaluationParam -> {
            this.removeById(sysAppInfoEvaluationParam.getId());
        });
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(SysAppInfoEvaluationParam sysAppInfoEvaluationParam) {
        SysAppInfoEvaluation sysAppInfoEvaluation = this.querySysAppInfoEvaluation(sysAppInfoEvaluationParam);
        BeanUtil.copyProperties(sysAppInfoEvaluationParam, sysAppInfoEvaluation);
        this.updateById(sysAppInfoEvaluation);
    }

    @Override
    public SysAppInfoEvaluation detail(SysAppInfoEvaluationParam sysAppInfoEvaluationParam) {
        return this.querySysAppInfoEvaluation(sysAppInfoEvaluationParam);
    }

    /**
     * 获取app评价
     *
     * @author yanwei
     * @date 2023-03-21 12:07:25
     */
    private SysAppInfoEvaluation querySysAppInfoEvaluation(SysAppInfoEvaluationParam sysAppInfoEvaluationParam) {
        SysAppInfoEvaluation sysAppInfoEvaluation = this.getById(sysAppInfoEvaluationParam.getId());
        if (ObjectUtil.isNull(sysAppInfoEvaluation)) {
            throw new ServiceException(SysAppInfoEvaluationExceptionEnum.NOT_EXIST);
        }
        return sysAppInfoEvaluation;
    }

    @Override
    public void export(SysAppInfoEvaluationParam sysAppInfoEvaluationParam) {
        List<SysAppInfoEvaluation> list = this.list(sysAppInfoEvaluationParam);
        PoiUtil.exportExcelWithStream("SnowySysAppInfoEvaluation.xls", SysAppInfoEvaluation.class, list);
    }

}
